﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace LongNet.Util
{
    public class CommonTools
    {
        static Random rdm = new Random();
        /// <summary>
        /// 弹出对户框
        /// </summary>
        /// <param name="p"></param>
        /// <param name="msg"></param>
        public static void Alert(Page p, string msg)
        {
            p.ClientScript.RegisterStartupScript(p.GetType(), rdm.Next(1000) + DateTime.Now.ToShortTimeString(), "alert('" + msg + "');", true);
        }
        /// <summary>
        /// 弹出对话框并返回到指定页面
        /// </summary>
        /// <param name="p"></param>
        /// <param name="msg"></param>
        /// <param name="url"></param>
        public static void Alert(Page p, string msg, string url)
        {
            p.ClientScript.RegisterStartupScript(p.GetType(), rdm.Next(1000) + DateTime.Now.ToShortTimeString(), "alert('" + msg + "');window.location.href='" + url + "';", true);
        }
        /// <summary>
        /// 生成业务编号
        /// </summary>
        /// <returns></returns>
        public static string GetBussinessNo()
        {
            string bussinessNo = DateTime.Now.Year + "";
            int month = DateTime.Now.Month;
            int day = DateTime.Now.Day;
            int hour = DateTime.Now.Hour;
            int minute = DateTime.Now.Minute;
            int sencond = DateTime.Now.Second;
            if (month >= 10)
            {
                bussinessNo += month + "";
            }
            else
            {
                bussinessNo += "0" + month;
            }
            if (day >= 10)
            {
                bussinessNo += day + "";
            }
            else
            {
                bussinessNo += "0" + day;
            }
            if (hour >= 10)
            {
                bussinessNo += hour + "";
            }
            else
            {
                bussinessNo += "0" + hour;
            }
            if (minute >= 10)
            {
                bussinessNo += minute + "";
            }
            else
            {
                bussinessNo += "0" + minute;
            }
            if (sencond >= 10)
            {
                bussinessNo += sencond + "";
            }
            else
            {
                bussinessNo += "0" + sencond;
            }

            int num = rdm.Next(1000);
            if (num < 10)
            {
                bussinessNo += "00" + num;
            }
            else if (num >= 10 && num < 100)
            {
                bussinessNo += "0" + num;
            }
            else
            {
                bussinessNo += num + "";
            }
            return bussinessNo;
        }
        /// <summary>
        /// 打开新窗口
        /// </summary>
        /// <param name="p"></param>
        /// <param name="strUrl"></param>
        public static void ShowDialog(Page p, string strUrl)
        {
            p.ClientScript.RegisterStartupScript(p.GetType(), rdm.Next(1000) + DateTime.Now.ToString(), "window.open('" + strUrl + "');", true);
        }
        /// <summary>
        /// 转换日期格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string ConvertDateTime(DateTime date)
        {
            return date.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 将字符串换行
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ReturnLine(string str)
        {
            int length = str.Length;
            string newStr = "";
            Char[] strs = str.ToCharArray();
            for (int i = 0; i < length; i++)
            {
                newStr += strs[i];
                if (i % 50 == 0 && i != 0)
                {
                    newStr += "<br>";
                }
            }
            return newStr;
        }
        /// <summary>
        /// 浏览或下载文件
        /// </summary>
        /// <param name="FileName"></param>
        public static void openFile(string FileName,Page p)
        {

            //打开要下载的文件 
            System.IO.FileStream r = new System.IO.FileStream(FileName, System.IO.FileMode.Open);
            //设置基本信息 
            p.Response.Buffer = false;
            p.Response.AddHeader("Connection", "Keep-Alive");
            p.Response.ContentType = "application/octet-stream";
            p.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.IO.Path.GetFileName(FileName));
            p.Response.AddHeader("Content-Length", r.Length.ToString());


            while (true)
            {
                //开辟缓冲区空间 
                byte[] buffer = new byte[1024];
                //读取文件的数据 
                int leng = r.Read(buffer, 0, 1024);
                if (leng == 0)//到文件尾，结束 
                    break;
                if (leng == 1024)//读出的文件数据长度等于缓冲区长度，直接将缓冲区数据写入 
                    p.Response.BinaryWrite(buffer);
                else
                {
                    //读出文件数据比缓冲区小，重新定义缓冲区大小，只用于读取文件的最后一个数据块 
                    byte[] b = new byte[leng];
                    for (int i = 0; i < leng; i++)
                        b[i] = buffer[i];
                    p.Response.BinaryWrite(b);
                }
            }
            r.Close();//关闭下载文件 
            p.Response.End();//结束文件下载 
        }
    }
}
